<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px">
        <el-form-item label="员工" prop="userId">
          <el-select v-model="ruleForm.userId" style="width: 100%">
            <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="登录状态" prop="loginStatus">
          <el-input v-model="ruleForm.loginStatus" maxlength="20" show-word-limit />
        </el-form-item>
        <el-form-item label="登录时间" prop="loginTime">
          <el-date-picker v-model="ruleForm.loginTime" type="datetime" placeholder="选择日期时间" style="width: 100%" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item label="登录地址" prop="loginUrl">
          <el-input v-model="ruleForm.loginUrl" maxlength="20" show-word-limit />
        </el-form-item>
      </el-form>
    </el-row>
    <!-- <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span> -->
  </el-dialog>
</template>
<script>
export default {
  props: {
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      const res = await this.$axios.get(`/userInfo/verifyNameUnique?name=${value}&id=${this.ruleForm.id || ''}`)
      if (res?.data.code === 200) {
        return res.data.data ? callback() : new Error(callback('员工姓名重复！'))
      } else {
        return new Error(callback(res.data.msg))
      }
    }
    return {
      title: '',
      deptList: [],
      ruleForm: {
        userId: '',
        loginTime: '',
        loginStatus: '',
        loginUrl: ''
      },
      rules: {
        userId: [
          { required: true, message: '请选择员工！', trigger: 'change' },
          { validator: checkName, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      departData: []
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      this.title = '新增'
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const res = await this.$axios.post('/sys/loginLog/save', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        userId: '',
        loginTime: '',
        loginStatus: '',
        loginUrl: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
